/*
 * Created on 17.10.2003
 * Local Projects Database
 * Mihai Postelnicu (mihai@ro-gateway.org)
 * (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.struts.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.tiles.actions.TilesAction;

import ro.gateway.aida.db.PersistenceManager;
import ro.gateway.aida.db.PersistenceToken;
import ro.gateway.aida.obj.Category;
import ro.gateway.aida.obj.db.CategoryDB;
import ro.gateway.aida.utils.BugTracker;
import ro.gateway.aida.utils.LocalisedSortableList;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;


/**
 * @author Mihai Postelnicu
 *  CategoriesSelectorAction
 *  e-mail (mihai@ro-gateway.org)
 * (comparator) 2003 by eRomania Gateway
 */
public class CategoriesSelectorAction extends TilesAction {
    public static final String PNAME_BEANS = "SELECTED_CATEGORIES";
    public static final String PNAME_catlists = "CATS Selected Hashtable";

    public ActionForward execute(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) {
        try {
            HttpSession session = request.getSession();
            ServletContext sc = getServlet().getServletContext();
			PersistenceToken token = PersistenceManager.tokenLookup( sc );

            CategoryDB cdb = CategoryDB.getManager(token);

            HashSet catlist;
            LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage(request,
                    sc);
            String lang = lang_bean.language;
            LocalisedSortableList categories = (LocalisedSortableList) session.getAttribute(
                    "categories");

            if (categories == null) {
                categories = cdb.getAllLocalized(lang);
                session.setAttribute("categories", categories);
            }

            //revive categories from session:
            catlist = (HashSet) session.getAttribute("catlist");

            if (catlist == null) {
                catlist = new HashSet();
            }

            if (request.getParameter("add") != null) {
                int id = Integer.parseInt(request.getParameter("catsel"));
                catlist.add(categories.get(id));
            }

            if (request.getParameter("end") != null) {
                session.removeAttribute("categories");
                request.setAttribute("selReady", new String());
            }

            if (request.getParameter("delete") != null) {
                String[] delcat = request.getParameterValues("delcat");

                if (delcat != null) {
                    for (int i = 0; i < delcat.length; i++)
                        catlist.remove(traceId(Integer.parseInt(delcat[i]),
                                categories));
                }
            }

            session.setAttribute("catlist", catlist);

            return mapping.findForward("catselect");
        } catch (Exception e) {
            BugTracker.publishBug(e, request);

            return mapping.findForward("fail");
        }
    }

    private Object traceId(int internalId, ArrayList cats) {
        Iterator it = cats.iterator();

        while (it.hasNext()) {
            Category element = (Category) it.next();

            if (element.getId() == internalId) {
                return element;
            }
        }

        return null;
    }
}
